<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { useUserStore } from '@/stores/user.ts'

const ruleFormRef = ref<FormInstance>()
const store = useUserStore()
const validateUsername = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入用户名'))
  } else {
    callback()
  }
}

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  userAccount: '',
  userPassword: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
  userAccount: [{ validator: validateUsername, trigger: 'blur' }],
  userPassword: [{ validator: validatePass, trigger: 'blur' }],
})


</script>

<template>
  <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    status-icon
    :rules="rules"
    label-width="auto"
    label-position="left"
    class="demo-ruleForm"
  >
    <el-form-item label="用户名" prop="username">
      <el-input v-model="ruleForm.userAccount" autocomplete="off" />
    </el-form-item>
    <el-form-item label="密码" prop="pass">
      <el-input v-model="ruleForm.userPassword" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" style="width: 100%" @click="store.login(ruleForm)"> 登录 </el-button>
    </el-form-item>
  </el-form>
</template>

<style scoped lang="scss"></style>
